﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Xna.Framework;
using System.IO.IsolatedStorage;

namespace Oiler
{
    public class Player
    {
        private double _money;
        private double _worldAcceptance;
        private double _discenders;
        private double _fallowers;
        private int _barrelPrice;
        private double _supply;
        private double _demand;

        public double PlayerMoney
        {
            get { return _money; }
            set { _money += value; }
        }
        public double WorldAcceptance
        {
            get { return _worldAcceptance; }
            set { _worldAcceptance = value; }
        }
        public double Fallowers
        {
            get { return _fallowers; }
            set { _fallowers = value; }
        }
        public double Discenders
        {
            get { return _discenders; }
            set { _discenders = value; }
        }
        public int BarrelPrice
        {
            get { return _barrelPrice; }
            set { _barrelPrice += value; }
        }
        public double Supply
        {
            get { return _supply; }
            set { _supply = value; }
        }
        public double Demand
        {
            get { return _demand; }
            set { _demand = value; }
        }

        public bool teleSelect = false;
        public bool radSelect = false;
        public bool newsSelect = false;
        public bool billSelect = false;
        public bool teleRun = false;
        public bool radRun = false;
        public bool newsRun = false;
        public bool billRun = false;
        public bool adChange = false;
        public bool adCanRun = false;
        public int count;
        public int monthCount;
        public int count2;
        public int adBoost = 0;
        public int temp;
        public double totalIncome;
        public double tempIncome;

        public void Initialize(IsolatedStorageSettings saveGame)
        {
            if (!saveGame.Contains("playersMoney"))
                PlayerMoney = 500000;

            if (!saveGame.Contains("fallowers"))
                Fallowers = 100;

            if (!saveGame.Contains("discenders"))
                Discenders = 100;

            if (!saveGame.Contains("oilPrice"))
                BarrelPrice = 40;

            if (!saveGame.Contains("supply"))
                Supply = 1;

            if (!saveGame.Contains("demand"))
                Demand = 500;
        }

        public void Update(OilDeposits[] depos, int realTime, bool isGameRunning, IsolatedStorageSettings saveGame, Date date, Slider cutOff)
        {
            if (Demand > Supply)
            {
                for (int i = 0; i < depos.Length; i++)
                {
                    totalIncome += depos[i].Income - depos[i].Upkeep;
                }
            }
            else
            {
                totalIncome = Demand * BarrelPrice;
                for (int i = 0; i < depos.Length; i++)
                {
                    totalIncome += -depos[i].Upkeep;
                }
            }

            //every second, while the game is running, update money to reflect income
            if (realTime % 1 == 0 && isGameRunning && Demand > Supply)
            {
                for (int i = 0; i < depos.Length; i++)
                {
                    PlayerMoney = depos[i].Income - depos[i].Upkeep;
                }
            }
            else
            {
                PlayerMoney = Demand * BarrelPrice;
                for (int i = 0; i < depos.Length; i++)
                {
                    PlayerMoney = -depos[i].Upkeep;
                }
            }

            tempIncome = totalIncome;
            totalIncome = 0;

            
            count2++;

            for (int i = 0; i < depos.Length; i++)
            {

                temp += depos[i].SellableOil;  
            }
            Supply = temp;
            temp = 0;

            Demand = (750000 + (monthCount * monthCount * monthCount) + (WorldAcceptance * WorldAcceptance)) / (BarrelPrice * BarrelPrice);

            if (count2 > 60)
            {
                monthCount++;
                
                count2 = 0;
            }

            WorldAcceptance = (Fallowers - Discenders);

            count++;
            if (count >= 150 && adCanRun)
            {
                Marketing(adBoost);
                
                count = 0;
            }



            //if statement to remove money per week
            //for ad costs
            //Money removed is multiplied by 5
            if (date.Day % 7 == 0 && adCanRun)
            {
                if (teleRun)
                    PlayerMoney = -120000;

                if (radRun)
                    PlayerMoney = -70000;

                if (newsRun)
                    PlayerMoney = -30000;

                if (billRun)
                    PlayerMoney = -5000;
            }

            if (PlayerMoney <= cutOff.Value && (int)cutOff.Value != 0)
            {
                adCanRun = false; teleRun = false; radRun = false; newsRun = false; billRun = false;
            }
        }

        public void Draw()
        {
        }

        public void Marketing(int adBoost)
        {
            Random rand = new Random();
            int temp = rand.Next(101);
            if (temp - WorldAcceptance + adBoost > 70 && Discenders > 0)
            {
                Fallowers += 1;
                Discenders -= 1;
            }
        }

        public void Save(IsolatedStorageSettings saveGame)
        {
            if (!saveGame.Contains("playersMoney"))
                saveGame.Add("playersMoney", PlayerMoney);
            else
                saveGame["playersMoney"] = PlayerMoney;

            if (!saveGame.Contains("accept"))
                saveGame.Add("accept", WorldAcceptance);
            else
                saveGame["accept"] = WorldAcceptance;

            if (!saveGame.Contains("fallowers"))
                saveGame.Add("fallowers", Fallowers);
            else
                saveGame["fallowers"] = Fallowers;

            if (!saveGame.Contains("discenders"))
                saveGame.Add("discenders", Discenders);
            else
                saveGame["discenders"] = Discenders;

            if (!saveGame.Contains("adBoost"))
                saveGame.Add("adBoost", adBoost);
            else
                saveGame["adBoost"] = adBoost;

            if (!saveGame.Contains("tvRun"))
                saveGame.Add("tvRun", teleRun);
            else
                saveGame["tvRun"] = teleRun;

            if (!saveGame.Contains("adRun"))
                saveGame.Add("adRun", adCanRun);
            else
                saveGame["adRun"] = adCanRun;

            if (!saveGame.Contains("radRun"))
                saveGame.Add("radRun", radRun);
            else
                saveGame["radRun"] = radRun;

            if (!saveGame.Contains("newsRun"))
                saveGame.Add("newsRun", newsRun);
            else
                saveGame["newsRun"] = newsRun;

            if (!saveGame.Contains("billRun"))
                saveGame.Add("billRun", billRun);
            else
                saveGame["billRun"] = billRun;

            saveGame.Save();
        }

        public void Load(IsolatedStorageSettings saveGame)
        {
            if (saveGame.Contains("playersMoney"))
                PlayerMoney = (double)saveGame["playersMoney"];

            if (saveGame.Contains("accept"))
                WorldAcceptance = (double)saveGame["accept"];

            if (saveGame.Contains("fallowers"))
                Fallowers = (double)saveGame["fallowers"];

            if (saveGame.Contains("discenders"))
                Discenders = (double)saveGame["discenders"];

            if (saveGame.Contains("adBoost"))
                adBoost = (int)saveGame["adBoost"];

            if (saveGame.Contains("tvRun"))
                teleRun = (bool)saveGame["tvRun"];
                
            if (saveGame.Contains("adRun"))
                adCanRun = (bool)saveGame["adRun"];

            if (saveGame.Contains("radRun"))
                radRun = (bool)saveGame["radRun"];

            if (saveGame.Contains("newsRun"))
                newsRun = (bool)saveGame["newsRun"];

            if (saveGame.Contains("billRun"))
                newsRun = (bool)saveGame["billRun"];
        }
    }
}
